{% extends "base.html" %}

{% block title %}学生管理 - 学生成绩管理系统{% endblock %}

{% block extra_css %}
<link rel="stylesheet" href="{{ url_for('static', filename='css/common.css') }}">
<!-- <link rel="stylesheet" href="{{ url_for('static', filename='css/admin/student.css') }}"> -->
{% endblock %}

{% block content %}
<div class="student-wrapper">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="page-title">
            <i class="fas fa-user-graduate text-primary"></i>
            学生管理
        </h2>
        <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addStudentModal">
            <i class="fas fa-plus"></i> 添加学生
        </button>
    </div>

    <div class="card mb-4">
        <div class="card-body">
            <div class="row">
                <div class="col-md-3">
                    <div class="input-group">
                        <span class="input-group-text"><i class="fas fa-search"></i></span>
                        <input type="text" class="form-control" id="searchInput" placeholder="搜索学号或姓名...">
                    </div>
                </div>
                <div class="col-md-3">
                    <select class="form-select" id="classFilter">
                        <option value="">所有班级</option>
                    </select>
                </div>
                <div class="col-md-1">
                    <button type="button" class="btn btn-outline-secondary w-100" onclick="resetFilters()">
                        <i class="fas fa-redo"></i> 重置
                    </button>
                </div>
            </div>
        </div>
    </div>

    <div class="card">
        <div class="card-header">
            <h5 class="card-title mb-0">
                <i class="fas fa-list"></i> 学生列表
                <span class="badge bg-primary ms-2" id="listCount">0</span>
            </h5>
        </div>
        <div class="card-body">
            <div id="loadingSpinner" class="text-center py-4">
                <div class="spinner-border text-primary" role="status">
                    <span class="visually-hidden">加载中...</span>
                </div>
                <p class="mt-2 text-muted">正在加载学生数据...</p>
            </div>

            <div class="table-responsive" id="studentTableContainer" style="display: none;">
                <table class="table table-hover" id="studentTable">
                    <thead class="table-light">
                        <tr>
                            <th>学号</th>
                            <th>姓名</th>
                            <th>班级</th>
                            <th>密码</th>
                            <th>创建时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody id="studentTableBody">
                    </tbody>
                </table>
            </div>

            <div id="emptyState" class="text-center py-5" style="display: none;">
                <i class="fas fa-user-graduate fa-3x text-muted mb-3"></i>
                <h5 class="text-muted">暂无学生数据</h5>
                <p class="text-muted">点击上方"添加学生"按钮开始添加学生</p>
            </div>
        </div>
    </div>

    <div aria-label="学生分页" class="mt-4">
        <ul class="pagination justify-content-center" id="pagination">
        </ul>
    </div>
</div>

<!-- 添加学生模态框 -->
<div class="modal fade" id="addStudentModal" tabindex="-1" aria-labelledby="addStudentModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="addStudentModalLabel">
                    <i class="fas fa-plus text-primary"></i> 添加学生
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="addStudentForm">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="addStudentSno" class="form-label">学号 <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="addStudentSno" name="sno" required
                                       placeholder="如：S123456" maxlength="50">
                                <div class="invalid-feedback">请输入学号</div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="addStudentName" class="form-label">姓名 <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="addStudentName" name="name" required
                                       placeholder="如：张三" maxlength="50">
                                <div class="invalid-feedback">请输入姓名</div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="addStudentClass" class="form-label">班级 <span class="text-danger">*</span></label>
                                <select class="form-select" id="addStudentClass" name="class_id" required>
                                    <option value="">请选择班级</option>
                                </select>
                                <div class="invalid-feedback">请选择班级</div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="addStudentPassword" class="form-label">密码 <span class="text-danger">*</span></label>
                                <input type="password" class="form-control" id="addStudentPassword" name="password" required
                                       placeholder="请输入密码" maxlength="50">
                                <div class="invalid-feedback">请输入密码</div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save"></i> 保存学生
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 编辑学生模态框 -->
<div class="modal fade" id="editStudentModal" tabindex="-1" aria-labelledby="editStudentModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="editStudentModalLabel">
                    <i class="fas fa-edit text-warning"></i> 编辑学生
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="editStudentForm">
                <div class="modal-body">
                    <input type="hidden" id="editStudentId" name="id">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="editStudentSno" class="form-label">学号 <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="editStudentSno" name="sno" required
                                       placeholder="如：S123456" maxlength="50">
                                <div class="invalid-feedback">请输入学号</div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="editStudentName" class="form-label">姓名 <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="editStudentName" name="name" required
                                       placeholder="如：张三" maxlength="50">
                                <div class="invalid-feedback">请输入姓名</div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="editStudentClass" class="form-label">班级 <span class="text-danger">*</span></label>
                                <select class="form-select" id="editStudentClass" name="class_id" required>
                                    <option value="">请选择班级</option>
                                </select>
                                <div class="invalid-feedback">请选择班级</div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="editStudentPassword" class="form-label">密码 <span class="text-danger">*</span></label>
                                <input type="password" class="form-control" id="editStudentPassword" name="password" required
                                       placeholder="请输入密码" maxlength="50">
                                <div class="invalid-feedback">请输入密码</div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-warning">
                        <i class="fas fa-save"></i> 更新学生
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 删除学生模态框 -->
<div class="modal fade" id="deleteStudentModal" tabindex="-1" aria-labelledby="deleteStudentModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="deleteStudentModalLabel">
                    <i class="fas fa-exclamation-triangle text-danger"></i> 确认删除
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p>您确定要删除学生 <strong id="deleteStudentInfo"></strong> 吗？</p>
                <div class="alert alert-warning">
                    <i class="fas fa-exclamation-triangle"></i>
                    <strong>警告：</strong>删除学生将同时删除该学生的所有成绩记录，此操作不可撤销！
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-danger" id="confirmDeleteBtn">
                    <i class="fas fa-trash"></i> 确认删除
                </button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script src="{{ url_for('static', filename='js/admin/student.js') }}"></script>
{% endblock %}